package com.liuwei.msmall.product.controller.api;

import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.R;
import com.liuwei.msmall.product.entity.ProductAttrValueEntity;
import com.liuwei.msmall.product.service.AttrService;
import com.liuwei.msmall.product.vo.AttrVo;
import com.liuwei.msmall.product.vo.resp.AttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author llww
 * @email 2456888577@qq.com
 * @date 2021-02-25 17:45:01
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;


    /**
     * 查询某商品的规格参数信息
     *
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseAttrBySpuId(@PathVariable("spuId") Long spuId){

       List<ProductAttrValueEntity> datas = attrService.getBaseAtttrListBySpuId(spuId);

        return R.ok().setData(datas);
    }




    /**
     * 获取属性列表
     */
    @RequestMapping("/{type}/list/{cateId}")
    public R list(@PathVariable("type") String type,
                  @PathVariable(value = "cateId",required = false) Long cateId,
                  @RequestParam Map<String, Object> params){

        PageUtils page = attrService.queryPage(type,cateId,params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){

//		AttrEntity attr = attrService.getById(attrId);

		AttrRespVo vo =  attrService.getAttrInfoById(attrId);

        return R.ok().put("attr", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){

        attrService.saveDetail(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){


		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 修改商品的规格参数信息
     */
    @PostMapping("/update/{spuId}")
    public R updateBaseAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> attrs){

        attrService.updateBaseAttrBySpuId(spuId,attrs);

        return R.ok();
    }





    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
